package demo.xlj.yuyi.studentapp.common;

import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import demo.xlj.yuyi.studentapp.R;
import demo.xlj.yuyi.studentapp.base.BaseRxActivity;
import demo.xlj.yuyi.studentapp.bean.LookHuoDongBean;
import demo.xlj.yuyi.studentapp.bean.MyDelInfoBean;
import demo.xlj.yuyi.studentapp.myadapter.MyHuoDongShenAdapter;
import demo.xlj.yuyi.studentapp.retrofit.RetrofitClient;
import demo.xlj.yuyi.studentapp.rxjava.RxSchedulers;
import demo.xlj.yuyi.studentapp.shetuan.ApplyHuoDongActivity;
import demo.xlj.yuyi.studentapp.utils.ActivityUtil;
import demo.xlj.yuyi.studentapp.utils.SharePerferencesUtils;
import demo.xlj.yuyi.studentapp.utils.ToastUtil;
import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Consumer;

/**
 * 作者：项路俊 on 2018/3/2 0002
 * 邮箱：ThanksForCode@gmail.com
 * 介绍：查看我的申请
 */

public class MyCommicationHuoDongActivity extends BaseRxActivity {


    @BindView(R.id.ImageView_back)
    ImageView mImageViewBack;
    @BindView(R.id.TextView_Title)
    TextView mTextViewTitle;
    @BindView(R.id.RecyclerView_my_commication_huo_dong)
    RecyclerView mRecyclerViewMyCommicationHuoDong;

    private List<LookHuoDongBean.InfoBean> mList = new ArrayList<>();

    private MyHuoDongShenAdapter mMyHuoDongShenAdapter;


    private String userid;

    private AlertDialog.Builder mBuilder;

    private TextView info_s;

    private String info_text;



    private AlertDialog.Builder mBuilder_2;

    private Handler mHandler = new Handler(){

        @Override
        public void handleMessage(final Message msg) {
            super.handleMessage(msg);
            if (msg.what == 0){
                addRxDestroy(RetrofitClient.getInstance(MyCommicationHuoDongActivity.this)
                        .getApiService()
                        .delAll(mList.get(msg.arg1).getId()+"")
                        .compose(RxSchedulers.<MyDelInfoBean>io_main())
                        .subscribe(new Consumer<MyDelInfoBean>() {
                            @Override
                            public void accept(@NonNull MyDelInfoBean myDelInfoBean) throws Exception {
                                if (myDelInfoBean.getStatus() == 10088){
                                    ToastUtil.success(MyCommicationHuoDongActivity.this,"删除成功",true,true);
                                    mList.remove(msg.arg1);
                                    if (mList.size() == 0){
                                        mMyHuoDongShenAdapter.setEmptyView(R.layout.recycler_item_empty,mRecyclerViewMyCommicationHuoDong);
                                    }
                                    mMyHuoDongShenAdapter.notifyDataSetChanged();
                                }else {
                                    ToastUtil.error(MyCommicationHuoDongActivity.this,"删除失败",false,false);
                                    mMyHuoDongShenAdapter.notifyDataSetChanged();
                                }

                            }
                        }, new Consumer<Throwable>() {
                            @Override
                            public void accept(@NonNull Throwable throwable) throws Exception {
                                ToastUtil.error(MyCommicationHuoDongActivity.this,"操作失败",true,true);
                            }
                        }));
            }
        }
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_commication_huo_dong);
        ButterKnife.bind(this);
        userid = (String) SharePerferencesUtils.get(MyCommicationHuoDongActivity.this, "name", "99", 0);
        mTextViewTitle.setText("我的申请");
        initRecyclerView();
        initData();
        initListener();

    }

    private void initListener() {
        mMyHuoDongShenAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, final int position) {
                switch (mList.get(position).getIsok()) {
                    case 0:
                        final AlertDialog.Builder  mBuilder = new AlertDialog.Builder(MyCommicationHuoDongActivity.this);
                        mBuilder.setTitle("审核失败原因");
                        View ss = LayoutInflater.from(MyCommicationHuoDongActivity.this).inflate(R.layout.edit_text_why, null, false);
                        info_s = (TextView) ss.findViewById(R.id.TextView_edit_why_no);
                        if (mList.get(position).getReason() == null){
                            info_s.setText("暂未查询到原因");
                        }else {
                            info_s.setText(mList.get(position).getReason());
                        }
                        mBuilder.setView(ss);
                        mBuilder.setNegativeButton("重新申请", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                String keyss[] = {"content","who","address","time"};
                                String valuess[] = {mList.get(position).getContent(), (String) mList.get(position).getUndertake(),mList.get(position).getAddr(),mList.get(position).getTime()};
                                ActivityUtil.openActivityFromLeftToRightByMoreResuslt(MyCommicationHuoDongActivity.this, ApplyHuoDongActivity.class,keyss,valuess);
                            }
                        });
                        mBuilder.setPositiveButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                ToastUtil.info(MyCommicationHuoDongActivity.this, "您选择了取消", false, false);
                            }
                        });
                        mBuilder.show();
                        break;

                    case 1:
                        ToastUtil.success(MyCommicationHuoDongActivity.this, "您的申请正在认证", false, true);

                        break;

                    case 2:
                        ToastUtil.success(MyCommicationHuoDongActivity.this, "您的申请已通过初审，请等待复审！", false, true);
                        break;

                    case 3:
                        ToastUtil.success(MyCommicationHuoDongActivity.this, "审核通过，请准时开展活动", false, true);
                        break;
                }
            }
        });

        mMyHuoDongShenAdapter.setOnItemLongClickListener(new BaseQuickAdapter.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(BaseQuickAdapter adapter, View view, final int position) {
                if (mList.get(position).getIsok() == 0 ||mList.get(position).getIsok() == 3){
                    mBuilder_2 = new AlertDialog.Builder(MyCommicationHuoDongActivity.this);
                    mBuilder_2.setTitle("提示");
                    mBuilder_2.setMessage("删除的数据无法恢复哦！");
                    mBuilder_2.setPositiveButton("确定删除", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            Message  ms = new Message();
                            ms.what = 0;
                            ms.arg1 = position;
                            mHandler.sendMessage(ms);
                        }
                    });
                    mBuilder_2.setNegativeButton("取消删除", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                             ToastUtil.info(MyCommicationHuoDongActivity.this,"您取消了操作",false,false);
                        }
                    });
                    mBuilder_2.show();
                }else {
                    ToastUtil.info(MyCommicationHuoDongActivity.this,"你提交的活动正在申请中，不能删除",true,true);
                }
                return true;
            }
        });
    }

    private void initData() {
        mMyHuoDongShenAdapter.setEmptyView(R.layout.recycler_loading,mRecyclerViewMyCommicationHuoDong);
        mList.clear();
        mMyHuoDongShenAdapter.notifyDataSetChanged();
        addRxDestroy(RetrofitClient.getInstance(MyCommicationHuoDongActivity.this)
                .getApiService()
                .lookHuoDongForSheTuan(userid)
                .compose(RxSchedulers.<LookHuoDongBean>io_main())
                .subscribe(new Consumer<LookHuoDongBean>() {
                    @Override
                    public void accept(@NonNull LookHuoDongBean lookHuoDongBean) throws Exception {
                        if (lookHuoDongBean.getInfo().size() == 0) {
                            mMyHuoDongShenAdapter.setEmptyView(R.layout.recycler_item_empty, mRecyclerViewMyCommicationHuoDong);
                        } else {
                            mList.addAll(lookHuoDongBean.getInfo());
                            mMyHuoDongShenAdapter.notifyDataSetChanged();
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(@NonNull Throwable throwable) throws Exception {
                        ToastUtil.error(MyCommicationHuoDongActivity.this, "获取活动信息失败", true, true);
                        mMyHuoDongShenAdapter.setEmptyView(R.layout.recycler_item_empty, mRecyclerViewMyCommicationHuoDong);
                    }
                }));
    }

    private void initRecyclerView() {
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(MyCommicationHuoDongActivity.this);
        mRecyclerViewMyCommicationHuoDong.setLayoutManager(layoutManager);

        mMyHuoDongShenAdapter = new MyHuoDongShenAdapter(R.layout.recycler_new_item,mList);
        mMyHuoDongShenAdapter.setEmptyView(R.layout.recycler_loading, mRecyclerViewMyCommicationHuoDong);
        mRecyclerViewMyCommicationHuoDong.setAdapter(mMyHuoDongShenAdapter);

    }

    @OnClick(R.id.ImageView_back)
    public void onViewClicked() {
        MyCommicationHuoDongActivity.this.finish();
    }
}
